package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Yorumlar;
import hibernate.service.EmmyService;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("YorumlarBean")
@Scope("session")
public class YorumlarBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Yorumlar selectedYorum;	
	private List<Yorumlar> yorumList;
	
	private String yorumTrhBitis;
	
	@Override
	@PostConstruct
	public void init(){		
		//yorumList = (List<Yorumlar>) emmyService.findAll(new Yorumlar());
	}	
	
	@Override
	public String restart(){
		init();
		return "list";
	}

	@Override
	public String deleteView(){
		emmyService.delete(selectedYorum.getYorumid());
		init();
		return "list";
	}

	@Override
	public String viewNew() {
		selectedYorum= new Yorumlar();
		return "detail";
	}

	@Override
	public String viewDetail() {/*
		List<Yorumlar> valueList = (List<Yorumlar>) yorumTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = yorumTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentYorum= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentYorum.getYorumid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		emmyService.saveOrUpdate(selectedYorum);
		init();
		return "list";
	}
	
	@SuppressWarnings("unchecked")
	public String getDetayliYorum(){
			yorumList = (List<Yorumlar>) emmyService.getQuery(createQuery());
		return "list";
	}

	@Override
	public String createQuery() {
		Yorumlar yrm  = selectedYorum;		
		String query = "from Yorumlar";				
		
		if(yrm == null){
			return query;
		}
		if(yrm.getMusteri().getKullaniciid() != 0){
			if(query.indexOf("where") != -1){
				query += " and kullaniciid="+yrm.getMusteri().getKullaniciid()+"";
				
			}
			else{
				query += " where kullaniciid="+yrm.getMusteri().getKullaniciid()+"";			
			}
		}
		if(yrm.getPuan() != 0){
			if(query.indexOf("where") != -1){
				query += " and puan="+yrm.getPuan()+"";
				
			}
			else{
				query += " where puan="+yrm.getPuan()+"";			
			}
		}
		if(yrm.getYorum() !=null){
			if(query.indexOf("where") != -1){
				query += " and yorum like '%"+yrm.getYorum()+"%'";
				
			}
			else{
				query += " where yorum like '%"+yrm.getYorum()+"%'";			
			}
		}
		if(yrm.getAygit().getAygitid() !=0){
			if(query.indexOf("where") != -1){
				query += " and aygitid="+yrm.getAygit().getAygitid()+"";
				
			}
			else{
				query += " where aygitid="+yrm.getAygit().getAygitid()+"";			
			}
		}
		if(yrm.getBaslik() !=null){
			if(query.indexOf("where") != -1){
				query += " and baslik like '%"+yrm.getBaslik()+"%'";
				
			}
			else{
				query += " where baslik like '%"+yrm.getBaslik()+"%'";			
			}
		}
		if(yrm.getTarih() != null){
			if(query.indexOf("where") != -1){
				query += " and (tarih between "+yrm.getTarih()+" and "+yorumTrhBitis+")";
				
			}
			else{
				query += " where (tarih between "+yrm.getTarih()+" and "+yorumTrhBitis+")";			
			}
		}		
		return query;
	}
	
	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public Yorumlar getSelectedYorum() {
		return selectedYorum;
	}

	public void setSelectedYorum(Yorumlar selectedYorum) {
		this.selectedYorum = selectedYorum;
	}

	public List<Yorumlar> getYorumList() {
		return yorumList;
	}

	public void setYorumList(List<Yorumlar> yorumList) {
		this.yorumList = yorumList;
	}

	public String getYorumTrhBitis() {
		return yorumTrhBitis;
	}

	public void setYorumTrhBitis(String yorumTrhBitis) {
		this.yorumTrhBitis = yorumTrhBitis;
	}	
}